package com.alivc.testmediaplayer.activity;

import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.WindowManager;
import android.widget.Toast;

import com.alivc.player.AliVcMediaPlayer;
import com.alivc.testmediaplayer.R;
import com.alivc.testmediaplayer.utils.CleanLeakUtils;


public class ListModeActivity extends AppCompatActivity  {

    public static final String URL = "http://player.alicdn.com/video/aliyunmedia.mp4";

    private AliVcMediaPlayer mPlayerOne, mPlayerTwo, mPlayerThree, mPlayerFour, mPlayerFive;
    private TextureView mSurfaceViewOne, mSurfaceViewTwo, mSurfaceViewThree, mSurfaceViewFour, mSurfaceViewFive;
    private SurfaceTexture surfaceTextureOne,surfaceTextureTwo,surfaceTextureThree,surfaceTextureFour,surfaceTextureFive;
    private Handler mHandler;

    private boolean CancelMember = false;

    private Runnable r1 = new Runnable() {
        public void run() {
            if (!CancelMember) {
                startPlayerOne();
            }
        }
    };
    private Runnable r2 = new Runnable() {
        public void run() {
            if (!CancelMember) {
                startPlayerTwo();
            }
        }
    };
    private Runnable r3 = new Runnable() {
        public void run() {
            if (!CancelMember) {
                startPlayerThree();
            }
        }
    };
    private Runnable r4 = new Runnable() {
        public void run() {
            if (!CancelMember) {
                startPlayerFour();
            }
        }
    };

    private Runnable r5 = new Runnable() {
        public void run() {
            if (!CancelMember) {
                startPlayerFive();
            }
        }
    };

    private class MySurfaceTextureListener implements TextureView.SurfaceTextureListener
    {

        private TextureView mTextureView;

        public MySurfaceTextureListener(TextureView textureView){
            mTextureView = textureView;
        }

        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
            String textureViewName = "";
            if(mTextureView == mSurfaceViewOne){
                textureViewName = "mSurfaceViewOne";
                if(surfaceTextureOne != null) {
                    mSurfaceViewOne.setSurfaceTexture(surfaceTextureOne);
                }
            }else if(mTextureView == mSurfaceViewTwo){
                textureViewName = "mSurfaceViewTwo";
                if(surfaceTextureTwo != null)
                    mSurfaceViewTwo.setSurfaceTexture(surfaceTextureTwo);
            }else if(mTextureView == mSurfaceViewThree){
                textureViewName = "mSurfaceViewThree";
                if(surfaceTextureThree != null)
                    mSurfaceViewThree.setSurfaceTexture(surfaceTextureThree);
            }else if(mTextureView == mSurfaceViewFour){
                textureViewName = "mSurfaceViewFour";
                if(surfaceTextureFour != null)
                    mSurfaceViewFour.setSurfaceTexture(surfaceTextureFour);
            }else if(mTextureView == mSurfaceViewFive){
                textureViewName = "mSurfaceViewFive";
                if(surfaceTextureFive != null)
                    mSurfaceViewFive.setSurfaceTexture(surfaceTextureFive);
            }
            Log.d("lfj0929" , "onSurfaceTextureAvailable == " + textureViewName );
        }

        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {

        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {

            String textureViewName = "";
            if(mTextureView == mSurfaceViewOne){
                textureViewName = "mSurfaceViewOne";
            }else if(mTextureView == mSurfaceViewTwo){
                textureViewName = "mSurfaceViewTwo";
            }else if(mTextureView == mSurfaceViewThree){
                textureViewName = "mSurfaceViewThree";
            }else if(mTextureView == mSurfaceViewFour){
                textureViewName = "mSurfaceViewFour";
            }else if(mTextureView == mSurfaceViewFive){
                textureViewName = "mSurfaceViewFive";
            }
            Log.d("lfj0929" , "onSurfaceTextureDestroyed == " + textureViewName );
            return false;
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
//            String textureViewName = "";
//            if(mTextureView == mSurfaceViewOne){
//                textureViewName = "mSurfaceViewOne";
//            }else if(mTextureView == mSurfaceViewTwo){
//                textureViewName = "mSurfaceViewTwo";
//            }else if(mTextureView == mSurfaceViewThree){
//                textureViewName = "mSurfaceViewThree";
//            }else if(mTextureView == mSurfaceViewFour){
//                textureViewName = "mSurfaceViewFour";
//            }else if(mTextureView == mSurfaceViewFive){
//                textureViewName = "mSurfaceViewFive";
//            }
//            Log.d("lfj0929" , "onSurfaceTextureUpdated == " + textureViewName );
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_player);


        if(TextUtils.isEmpty(URL)){
            Toast.makeText(getApplicationContext(),R.string.url_is_empty,Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        //Texture open hardware accelecrate.
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        mHandler = new Handler();

        mSurfaceViewOne = (TextureView) findViewById(R.id.PlayerOne);
        mSurfaceViewOne.setSurfaceTextureListener(new MySurfaceTextureListener(mSurfaceViewOne));
        mSurfaceViewTwo = (TextureView) findViewById(R.id.PlayerTwo);
        mSurfaceViewTwo.setSurfaceTextureListener(new MySurfaceTextureListener(mSurfaceViewTwo));
        mSurfaceViewThree = (TextureView) findViewById(R.id.PlayerThree);
        mSurfaceViewThree.setSurfaceTextureListener(new MySurfaceTextureListener(mSurfaceViewThree));
        mSurfaceViewFour = (TextureView) findViewById(R.id.PlayerFour);
        mSurfaceViewFour.setSurfaceTextureListener(new MySurfaceTextureListener(mSurfaceViewFour));
        mSurfaceViewFive = (TextureView) findViewById(R.id.PlayerFive);
        mSurfaceViewFive.setSurfaceTextureListener(new MySurfaceTextureListener(mSurfaceViewFive));

        mHandler.postDelayed(r1, 1000);
        mHandler.postDelayed(r2, 5000);
        mHandler.postDelayed(r3, 10000);
        mHandler.postDelayed(r4, 15000);
        mHandler.postDelayed(r5, 20000);

    }

    private void startPlayerOne() {
        surfaceTextureOne = mSurfaceViewOne.getSurfaceTexture();
        mPlayerOne = new AliVcMediaPlayer(getApplicationContext() ,new Surface(surfaceTextureOne));
        mPlayerOne.prepareAndPlay(URL);

    }

    private void startPlayerTwo() {
        surfaceTextureTwo = mSurfaceViewTwo.getSurfaceTexture();
        mPlayerTwo = new AliVcMediaPlayer(getApplicationContext() ,new Surface(surfaceTextureTwo));
        mPlayerTwo.prepareAndPlay(URL);
    }

    private void startPlayerThree() {
        surfaceTextureThree = mSurfaceViewThree.getSurfaceTexture();
        mPlayerThree = new AliVcMediaPlayer(getApplicationContext() , new Surface(surfaceTextureThree));

        mPlayerThree.prepareAndPlay(URL);
    }

    private void startPlayerFour() {
         surfaceTextureFour = mSurfaceViewFour.getSurfaceTexture();
        mPlayerFour = new AliVcMediaPlayer(getApplicationContext(), new Surface(surfaceTextureFour));
        mPlayerFour.prepareAndPlay(URL);
    }

    private void startPlayerFive() {
         surfaceTextureFive = mSurfaceViewFive.getSurfaceTexture();
        mPlayerFive = new AliVcMediaPlayer(getApplicationContext(),new Surface(surfaceTextureFive));
        mPlayerFive.prepareAndPlay(URL);
    }

    @Override
    protected void onDestroy() {

        mHandler.removeCallbacks(r1);
        mHandler.removeCallbacks(r2);
        mHandler.removeCallbacks(r3);
        mHandler.removeCallbacks(r4);
        mHandler.removeCallbacks(r5);
        mHandler = null;

        CancelMember = true;

        if(mPlayerOne != null) {
            mPlayerOne.releaseVideoSurface();
            mPlayerOne.stop();
            mPlayerOne.destroy();
            mPlayerOne = null;
        }
        if(mPlayerTwo != null) {
            mPlayerTwo.releaseVideoSurface();
            mPlayerTwo.stop();
            mPlayerTwo.destroy();
            mPlayerTwo = null;
        }
        if(mPlayerThree != null) {
            mPlayerThree.releaseVideoSurface();
            mPlayerThree.stop();
            mPlayerThree.destroy();
            mPlayerThree = null;
        }
        if(mPlayerFour != null) {
            mPlayerFour.releaseVideoSurface();
            mPlayerFour.stop();
            mPlayerFour.destroy();
            mPlayerFour = null;
        }
        if(mPlayerFive != null) {
            mPlayerFive.releaseVideoSurface();
            mPlayerFive.stop();
            mPlayerFive.destroy();
            mPlayerFive = null;
        }

        CleanLeakUtils.fixInputMethodManagerLeak(this);
        super.onDestroy();


        return;
    }



}
